//
//  HTMRECAd.swift
//  MagaCore
//
//  Created by MrMessy on 2023/5/19.
//

import SwiftUI
import AppLovinSDK

struct HTMRECAd: UIViewRepresentable {
  func makeUIView(context: Context) -> MAAdView {
    let var_adView = MAAdView(adUnitIdentifier: HTConfig.STATIC_MRECId, adFormat: MAAdFormat.mrec)
    var_adView.delegate = context.coordinator

    // Set background or background color for banners to be fully functional
    var_adView.backgroundColor = UIColor.clear

    // Load the first Ad
    var_adView.loadAd()

    return var_adView
  }

  func updateUIView(_ uiView: MAAdView, context: Context) {}

  func makeCoordinator() -> Coordinator {
    Coordinator()
  }
}

extension HTMRECAd {
  class Coordinator: NSObject, MAAdViewAdDelegate {
    // MARK: MAAdDelegate Protocol

    func didLoad(_ ad: MAAd) {}

    func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {}

    func didClick(_ ad: MAAd) {}

    func didFail(toDisplay ad: MAAd, withError error: MAError) {}

    // MARK: MAAdViewAdDelegate Protocol

    func didExpand(_ ad: MAAd) {}

    func didCollapse(_ ad: MAAd) {}

    // MARK: Deprecated Callbacks

    func didDisplay(_ ad: MAAd) { /* use this for impression tracking */ }
    func didHide(_ ad: MAAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
  }
}
